"use strict";
cc._RF.push(module, 'b2d79oiyFBOdpfhpoFJX+hn', 'game');
// scripts/game.js

'use strict';

// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        wallPrefab: {
            default: null,
            type: cc.Prefab
        },
        btn: {
            default: null,
            type: cc.Prefab
        },
        topView: {
            default: null,
            type: cc.Node
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad: function onLoad() {
        this.walls = [];
        for (var i = 0; i < 4; i++) {
            var newBtn = cc.instantiate(this.btn);
            var x = (this.node.width - 444) / 5 + i * 111 - 320;
            // console.log(x);
            var y = -330;
            newBtn.setPosition(x, y);
            newBtn.getComponent('btn').game = this;
            this.node.addChild(newBtn);
        }
        this.timer = 0;
    },
    createWall: function createWall(randX, randY) {
        // alert(2)
        // 使用给定的模板在场景中生成一个新节点
        var newWall = cc.instantiate(this.wallPrefab);
        // alert(newWall)
        // 将新增的节点添加到 Canvas 节点下面
        this.node.addChild(newWall);
        // 为星星设置一个随机位置
        newWall.setPosition(this.getNewWallPosition(randX, randY));
        newWall.getComponent('wall').game = this;
        var height = 380;
        var wallActionSecond = 1;
        var currentHeight = 450;
        for (var i = 0; i < this.walls.length; i++) {
            var wall = this.walls[i];
            // var canGoTop = wall.getComponent('wall').canGoTop;
            var targetPointY = wall.getComponent('wall').targetPointY;
            // console.log(targetPointY);
            if (wall.x === randX && currentHeight > targetPointY) {
                height = targetPointY - 70;
                currentHeight = targetPointY;
            }
        }

        wallActionSecond = Math.abs(height + 330) * 0.0026;

        // console.log(height);
        newWall.getComponent('wall').targetPointY = height;
        newWall.getComponent('wall').runToPoint(wallActionSecond, randX, height);
        this.walls.push(newWall);
    },
    randomCreateWall: function randomCreateWall(randX, randY) {
        // alert(2)
        // 使用给定的模板在场景中生成一个新节点
        var newWall = cc.instantiate(this.wallPrefab);
        // alert(newWall)
        // 将新增的节点添加到 Canvas 节点下面
        this.node.addChild(newWall);
        // 为星星设置一个随机位置
        newWall.setPosition(this.getNewWallPosition(randX, randY));
        newWall.getComponent('wall').game = this;
        var height = 380;
        var wallActionSecond = 0;

        wallActionSecond = Math.abs(height + 330) * 0.0026;

        // console.log(wallActionSecond);
        newWall.getComponent('wall').targetPointY = height;
        newWall.getComponent('wall').fromTop = true;
        newWall.getComponent('wall').runToPoint(wallActionSecond, randX, height);
        this.walls.push(newWall);
    },
    getNewWallPosition: function getNewWallPosition(randX, randY) {
        // 返回星星坐标
        return cc.v2(randX, randY);
    },
    start: function start() {},
    update: function update(dt) {
        if (this.timer > 3) {
            for (var i = 0; i < this.walls.length; i++) {
                var wall = this.walls[i];
                if (wall.getComponent('wall').beginDestroy) {
                    this.timer--;
                    return;
                }
            }
            // for (var i = 0; i < 4; i++) {
            // for (var i = 0; i < this.walls.length; i++) {
            //     var wall = this.walls[i];
            //     if (wall.getComponent('wall').onTop) {
            //         wall.setPosition(wall.x, wall.y - 50 - 20);
            //         this.walls.splice(i, wall);
            //     }
            // }
            var wallActionSecond = 0;
            var tempWalls = this.walls;
            for (var i = 0; i < tempWalls.length; i++) {
                var wall = tempWalls[i];
                // var canGoTop = wall.getComponent('wall').canGoTop;
                var targetPointY = wall.getComponent('wall').targetPointY;
                // console.log(targetPointY);
                // if (wall.x === randX) {
                wallActionSecond = Math.abs(targetPointY - wall.y) * 0.0026;
                var currentTargetPointY = targetPointY - 70;
                var canGoTop = wall.getComponent('wall').canGoTop;
                wall.getComponent('wall').targetPointY = currentTargetPointY;
                if (canGoTop) {
                    wall.getComponent('wall').runStop();
                    wall.getComponent('wall').runToPoint(wallActionSecond, wall.x, currentTargetPointY);
                } else {

                    wall.getComponent('wall').runToPoint(wallActionSecond, wall.x, currentTargetPointY);
                }

                this.walls.splice(i, 1, wall);
            }

            var index = Math.floor(Math.random() * 4);
            // var currentBottom = 380;
            for (var i = 0; i < 4; i++) {
                if (index !== i) {
                    var x = (this.node.width - 444) / 5 + i * 111 - 320;
                    this.randomCreateWall(x, 450);
                }
            }

            this.timer = 0;
        }
        this.timer += dt;
    }
});

cc._RF.pop();